import {
    GET_ACCOUNT_DETAIL, GET_BANKINFO_DETAIL, SET_BANKINFO_DETAIL, APPLY_CRASH,
    GET_RECORD_LIST, GET_TRANSATION_LIST, CHOOSE_BANK
} from './constants';

const initState = {
    accountId: '',
    activeAmount: (0).toFixed(2),
    frozenAmount: (0).toFixed(2),
    totalAmount: (0).toFixed(2),
    amount: (0).toFixed(2),
    // bankId: '请输入银行卡号',
    bankId: '',
    // bankName: '点击获取开户银行',
    bankName: '',
    accountName: '',
    // bankArea: '开户地址',
    bankArea: '',
    // bankDetail: '开户支行',
    bankDetail: '',
    recordAccountId: '',
    recordList: [],
    transationList: [],

};

const applycrash = (params, state) => {
    let active = state.activeAmount - params.applyMoney;
    let frozen = state.frozenAmount + params.applyMoney;
    const newdata = {
        activeAmount: active,
        frozenAmount: frozen
    };
    return newdata;
};


const reducer = (state = initState, action) => {
    switch (action.type) {
        case GET_ACCOUNT_DETAIL:
            return Object.assign({}, state, action.payload);
        case GET_BANKINFO_DETAIL:
            return Object.assign({}, state, action.payload);
        case SET_BANKINFO_DETAIL:
            return Object.assign({}, state, action.payload);
        case APPLY_CRASH:
            let value = applycrash(action.payload, state);
            return Object.assign({}, state, value);
        case GET_RECORD_LIST:

            let index = action.payload.params.start;

            if (index == 0) {
                return Object.assign({}, state, action.payload);
            } else {

                let list = state.recordList;
                list = list.concat(action.payload.recordList);

                return Object.assign({}, state, {recordList: list})
            }
        case GET_TRANSATION_LIST:
            let index1 = action.payload.params.page;
            if (index1 == 1) {
                return Object.assign({}, state, action.payload);
            } else {
                let list = state.transationList;
                list = list.concat(action.payload.transationList);
                return Object.assign({}, state, {transationList: list});
            }
        case CHOOSE_BANK:
            return Object.assign({}, state, action.payload);
        case 'reset':
            return Object.assign({} , state , initState);
        default:
            return state;
    }
};


export default reducer;
